﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Toolkit.Helpers
{
    public class RandomDataProvider
    {
        public RandomDataProvider()
            : this(null)
        {
        }

        public RandomDataProvider(Random instance)
        {
            if (instance == null)
            {
                instance = new Random(Environment.TickCount);
            }

            RandomInstance = instance;
        }

        public Random RandomInstance
        {
            get;
            set;
        }

        public bool NextBoolean()
        {
            return (NextInteger(0, 1) == 0);
        }

        public int NextInteger()
        {
            return NextInteger(0, 100000);
        }

        public int NextInteger(int min, int max)
        {
            return RandomInstance.Next(min, max);
        }

        public double NextDouble()
        {
            return NextDouble(0, 100000);
        }

        public double NextDouble(double min, double max)
        {
            if (max < min)
            {
                throw new ArgumentException();
            }
            return ((max - min) * RandomInstance.NextDouble()) + min;
        }

        public decimal NextDecimal()
        {
            return NextDecimal(0, 100000);
        }

        public decimal NextDecimal(decimal min, decimal max)
        {
            return (decimal)NextDouble((double)min, (double)max);
        }

        public DateTime NextDateTime()
        {
            return NextDateTime(DateTime.Now.AddYears(-10), DateTime.Now.AddYears(10));
        }

        public DateTime NextDateTime(DateTime min, DateTime max)
        {
            if (max < min)
            {
                throw new ArgumentException();
            }
            return new DateTime(NextInteger(min.Year, max.Year), NextInteger(1, 12), NextInteger(1, 29), NextInteger(0, 23), NextInteger(0, 59), NextInteger(0, 59));
        }

        public TimeSpan NextTimeSpan()
        {
            return NextTimeSpan(TimeSpan.MinValue, TimeSpan.FromDays(365));
        }

        public TimeSpan NextTimeSpan(TimeSpan min, TimeSpan max)
        {
            if (max < min)
            {
                throw new ArgumentException();
            }
            double ticks = (((double)max.Ticks - (double)min.Ticks) * RandomInstance.NextDouble()) + (double)min.Ticks;
            return new TimeSpan((int)Math.Round(ticks));
        }

        public T NextEnum<T>() where T : struct
        {
            int[] values = (int[])Enum.GetValues(typeof(T));
            int index = NextInteger(0, (values.Length - 1));
            return (T)((object)values[index]);
        }

        public string NextLipsumParagraph()
        {
            string[] blobs = {
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer commodo ultricies felis, quis mollis dolor elementum id. Morbi dolor sem, dapibus eu consequat sed, ullamcorper fringilla lacus. Cras at mauris ligula, eu convallis libero. Donec sit amet nibh id neque condimentum dignissim nec congue enim. Nam eget ipsum at libero tincidunt volutpat at in justo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut in quam quam, quis tincidunt dolor. Fusce eu orci ut neque porta sagittis. Maecenas tristique commodo lorem, at facilisis turpis venenatis vel. Phasellus egestas, diam vitae porta viverra, nibh enim vulputate justo, at hendrerit massa libero sagittis elit.",
                "Nunc aliquam congue ornare. Quisque ultricies risus non elit condimentum malesuada. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam scelerisque euismod dolor eget varius. Quisque euismod nisi vitae nunc vehicula tristique. Phasellus at volutpat nibh. Ut nibh nisi, auctor et cursus pellentesque, bibendum vulputate ipsum.",
                "Nam tincidunt elit id ligula egestas eu congue felis luctus. Aliquam erat volutpat. In in arcu felis. Praesent placerat, enim vitae lacinia rutrum, arcu sem euismod velit, eget ullamcorper tortor nunc in nisl. Quisque sit amet elementum tortor. Maecenas rutrum nunc arcu. Nulla aliquet dolor vitae nibh feugiat vel porta turpis semper. Pellentesque eleifend massa in odio consequat eu scelerisque lorem mollis. Maecenas orci metus, congue vulputate rhoncus vitae, rhoncus ac neque.",
                "Maecenas tristique commodo lorem, at facilisis turpis venenatis vel. Phasellus egestas, diam vitae porta viverra, nibh enim vulputate justo, at hendrerit massa libero sagittis elit."
            };
            return blobs[NextInteger(0, 3)];
        }

        public string NextLipsumTitle()
        {
            string[] blobs = {
                "Morbi ac eros tincidunt velit fringilla sagittis sed nec elit.",
                "Nullam vitae ligula urna, id bibendum tortor.",
                "Etiam pharetra tortor in tortor rutrum eget volutpat odio sollicitudin.",
                "Praesent elementum interdum massa, a convallis augue mattis non.",
                "Morbi luctus posuere sapien, eget ultrices ipsum dignissim non.",
                "Nullam non diam enim, non ultricies nulla.",
                "Fusce eleifend scelerisque magna, eu commodo arcu cursus luctus.",
                "Quisque lacinia tortor sit amet ligula pellentesque dictum aliquet mauris iaculis.",
                "Mauris a sem ut mauris faucibus dignissim nec et elit.",
                "Phasellus sed purus eu velit hendrerit volutpat vitae et metus."
            };
            return blobs[NextInteger(0, 9)];
        }
    }
}
